package by.grsu.epam.service.entitiesservice.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import by.grsu.epam.dao.GenericDAO;
import by.grsu.epam.dao.entitiesdao.UserDAO;
import by.grsu.epam.dao.entitiesdao.UserRoleDAO;
import by.grsu.epam.domain.user.User;
import by.grsu.epam.domain.user.UserRole;
import by.grsu.epam.service.GenericServiceImpl;
import by.grsu.epam.service.entitiesservice.UserRoleService;

@Service
public class UserRoleServiceImpl extends GenericServiceImpl<UserRole, Long> implements UserRoleService{

	@Autowired
	private UserRoleDAO userRoleDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Override
	public GenericDAO<UserRole, Long> getDAO() {
		return userRoleDAO;
	}

	@Override
	@Transactional
	public UserRole findByAuthority(String auth) {
		return userRoleDAO.findByAuthority(auth);
	}
	
	@Override
	@Transactional
	public String findRoleByUsername(String username) {
		
		String role;
		User user = userDAO.findByUsername(username);
		role = user.getRoles().get(0).getAuthority();
		
		return role;
	}
	
}
